package com.fastCms.business.bim.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastCms.business.frame.constant.CommonConst;
import com.fastCms.business.bim.daos.DeviceMapper;
import com.fastCms.business.bim.pojo.entity.Device;
import com.fastCms.business.bim.service.DeviceService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设备主表 服务实现类
 * </p>
 *
 * @author liusq
 * @since 2021-03-25
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {

    @Override
    public Device findById(Integer deviceId) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        queryWrapper.eq("id", deviceId);
        return this.getOne(queryWrapper);
    }

    @Override
    public Device findByDeviceCode(String deviceCode) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_code", deviceCode);
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        return this.getOne(queryWrapper);
    }

    @Override
    public Device findByDeviceCode2(String deviceCode) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_code", deviceCode);
        return this.getOne(queryWrapper);
    }

    @Override
    public Device findByDeviceCodeAndState(int state, String deviceCode) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", state);
        queryWrapper.eq("device_code", deviceCode);
        return this.getOne(queryWrapper);
    }

    @Override
    public int countDeviceTypeIn(List<Integer> sumCount) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("device_type_id", sumCount);
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        return this.count(queryWrapper);
    }

    @Override
    public int countDeviceType(Integer deviceTypeId) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("device_type_id", deviceTypeId);
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        return this.count(queryWrapper);
    }

    @Override
    public List<Device> findByIds(ArrayList<Integer> deviceIds) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        queryWrapper.in("id", deviceIds);
        return this.list(queryWrapper);
    }

}
